package ar.edu.itba.it.raytracer.scene
import ar.edu.itba.it.raytracer.engine.{ Vector, Ray }

/**
 * Rays are computed as indicated by "Vector Algebra for Ray Tracing" by Sederberg(2005) 
 */
class Camera(eye: Vector, up: Vector, a: Vector, width: Int, height: Int, val xres: Int, val yres: Int) {
  private val ema = eye - a
  private val w = ema.normalized
  private val u = (up ** ema).normalized
  private val v = w ** u
  
  def ray(y: Int, x: Int) = {
    val cu = ((2 * x + 1) / (2.0 * xres) - 0.5) * width
    val cv = (-(2 * y + 1) / (2.0 * yres) + 0.5) * height
    val pij = a + u * cu + v * cv
    
    Ray(eye, (pij - eye).normalized)
  }
}